package org.black.phoneUtility.app;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TabWidget;

public class TabsFragmentPagerAdapter extends FragmentPagerAdapter implements
		TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
	private Context contetxt;
	private TabHost tabHost;
	private HorizontalScrollView horizontalScrollView;
	private ViewPager viewPager;
	private ArrayList<TabInfo> tabs = new ArrayList<TabInfo>();

	static final class TabInfo {
		private final String tag;
		private final Class<?> clss;
		private final Bundle args;

		TabInfo(String tag, Class<?> clazz, Bundle args) {
			this.tag = tag;
			this.clss = clazz;
			this.args = args;
		}

		public String getTag() {
			return this.tag;
		}
	}

	static class DummyTabFactory implements TabHost.TabContentFactory {
		private Context contetxt;

		public DummyTabFactory(Context context) {
			this.contetxt = context;
		}

		@Override
		public View createTabContent(String tag) {
			View v = new View(this.contetxt);
			v.setMinimumWidth(0);
			v.setMinimumHeight(0);
			return v;
		}
	}

	public TabsFragmentPagerAdapter(Activity activity,
			FragmentManager fragmentManager, TabHost tabHost,
			HorizontalScrollView horizontalScrollView, ViewPager pager) {
		super(fragmentManager);
		this.contetxt = activity;
		this.tabHost = tabHost;
		this.viewPager = pager;
		this.horizontalScrollView = horizontalScrollView;
		this.tabHost.setOnTabChangedListener(this);
		this.viewPager.setAdapter(this);
		this.viewPager.setOnPageChangeListener(this);
	}

	public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
		tabSpec.setContent(new DummyTabFactory(this.contetxt));
		String tag = tabSpec.getTag();

		TabInfo info = new TabInfo(tag, clss, args);
		this.tabs.add(info);
		this.tabHost.addTab(tabSpec);
		notifyDataSetChanged();
	}

	@Override
	public Fragment getItem(int position) {
		TabInfo info = tabs.get(position);
		return Fragment.instantiate(this.contetxt, info.clss.getName(),
				info.args);
	}

	@Override
	public int getCount() {
		return tabs.size();
	}

	@Override
	public void onPageScrollStateChanged(int state) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {
		if (horizontalScrollView != null) {
			View tabView = tabHost.getTabWidget().getChildAt(position);
			if (tabView != null) {
				final int width = horizontalScrollView.getWidth();
				final int scrollPos = tabView.getLeft()
						- (width - tabView.getWidth()) / 2;
				horizontalScrollView.scrollTo(scrollPos, 0);
			} else {
				horizontalScrollView.scrollBy(positionOffsetPixels, 0);
			}
		}
	}

	@Override
	public void onPageSelected(int position) {
		// Unfortunately when TabHost changes the current tab, it kindly
		// also takes care of putting focus on it when not in touch mode.
		// The jerk.
		// This hack tries to prevent this from pulling focus out of our
		// ViewPager.
		TabWidget widget = this.tabHost.getTabWidget();
		int oldFocusability = widget.getDescendantFocusability();
		widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
		this.tabHost.setCurrentTab(position);
		widget.setDescendantFocusability(oldFocusability);
	}

	@Override
	public void onTabChanged(String tabId) {
		int position = 0;
		int length = tabs.size();
		for (int i = 0; i < length; i++) {
			TabInfo tabInfo = tabs.get(i);
			if (tabInfo.getTag().equals(tabId) == true) {
				position = i;
				break;
			}
		}
		this.viewPager.setCurrentItem(position);
	}

}
